<?php
function ConnectDB(){
    $link = mysqli_connect("localhost","root","bananadine777","BrickCMS")or die("Error " . mysqli_error($link));
    return $link;
}
function DelTree($dir) {
    $files = glob( $dir . '*', GLOB_MARK );
    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            delTree( $file );
        else
            unlink( $file );
    }
    rmdir( $dir );
}
function DeletePreviousRender(){
    $ToDelete = scandir("/TemplateEngine/Template/");
    foreach($ToDelete as $Folder){
        DelTree($Folder);
    }
}
function CreateFolders(){
    $link = ConnectDB();
    $query = "SELECT Id From Layout";
    $result = mysqli_query($link,$query);
    $Bricks = mysqli_fetch_row();
    foreach($Bricks as $Brick){
        mkdir("/TemplateEngine/Template/$Brick");
    }
}
function RenderCSS(){
    $link = ConnectDB();
    $query = "SELECT Id From Layout";
    $result = mysqli_query($link,$query);
    $Bricks = mysqli_fetch_row();
    
    
    foreach($Bricks as $Brick){
        $query2 = "SELECT * FROM `layout' WHERE Id=$Brick";
        $result2 = mysqli_query($link,$query2);
        $row = mysqli_fetch_row($result2);
        $Id = $row['Id'];
        $Newline = $row['Newline'];
        $Width = $row['Width'];
        $Height = $row['Height'];
        $BorderWidth = $row['BorderWidth'];
        $BorderStyle = $row['BorderStyle'];
        $BorderColour = $row['BorderColour'];
        $GradientDirection = $row['GradientDirection'];
        $GradientColours = $row['GradientColours'];
        if(!$Newline){
            $Css = ".$Brick\{\n width:$Width; \n height:$Height; \n border-width:$BorderWidth; \n border-style:$BorderStyle \n border-colour:$BorderColour \n background: linear-gradient($GradientDirection, $GradientColours)";
            $File = fopen("/TemplateEngine/Template/$Brick/$Brick.css","W");
            fwrite($File,$Css);
        }
    }
}
function CreateNewLines(){
    $link = ConnectDB();
    $query = "SELECT Id From Layout";
    $result = mysqli_query($link,$query);
    $Bricks = mysqli_fetch_array();
    foreach($Bricks as $Brick){
        $query2 = "SELECT * FROM `layout' WHERE Id=$Brick"
        $result2 = mysqli_query($link,$query2);
        $row = mysqli_fetch_row($result2);
        $Newline = $row['Newline'];
        if($Newline){
            touch("/TemplateEngine/Template/$Brick/NEWLINE.txt");
        }
    }
}
function CopyFunctions(){
    $link = ConnectDB();
    $query = "SELECT Id From Layout";
    $result = mysqli_query($link,$query);
    $Bricks = mysqli_fetch_array();
    foreach($Bricks as $Brick){
        $query2 = "SELECT * FROM `layout' WHERE Id=$Brick"
        $result2 = mysqli_query($link,$query2);
        $row = mysqli_fetch_row($result2);
        $Newline = $row['Newline'];
        $BrickFunction = $row['BrickFunction'];
        $BrickGenerator = $row['BrickGenerator'];
        if(!$Newline && !$BrickGenerator){
            $Source = "TemplateEngine/Bricks/$BrickFunction/$BrickFunction.php";
            $destination = "/TemplateEngine/Template/$Brick/$brick.php";
        }
        if(!$Newline && !$BrickFunction){
            $Source = "TemplateEngine/BrickGenerators/$BrickGenerator/$BrickGenerator.php";
            $destination = "/TemplateEngine/Template/$Brick/$brick.php";
        }
    }
}
function MergeCSS(){
    $link = ConnectDB();
    $query = "SELECT Id From Layout";
    $result = mysqli_query($link,$query);
    $Bricks = mysqli_fetch_array();
    
    foreach($Bricks as $Brick){
        $query2 = "SELECT * FROM `layout' WHERE Id=$Brick"
        $result2 = mysqli_query($link,$query2);
        $row = mysqli_fetch_row($result2);
        $Newline = $row['Newline'];
        $CssPreRender = "/TemplateEngine/Template/prerender.css";
        $NewCss = fopen($CssPreRender,"a+");
        if(!$Newline){
            $File = file_get_contents("/TemplateEngine/Template/$Brick/$Brick.css");
            fwrite($NewCss,$File);
            fclose($File);
        }
    }
    fclose($NewCss);
}
function CrushCSS(){
    $CssPreRender = "/TemplateEngine/Template/prerender.css";
    $NewCss = fopen($CssPreRender,"a+");
    $File = file_get_contents("/TemplateEngine/static.css");
    fwrite($NewCss,$File);
    fclose($NewCss);
    require_once '\TemplateEngine\CssCrush\CssCrush.php'; 
    $compiled_file = csscrush_file("/TemplateEngine/Template/prerender.css");
    $RenderedCss = fopen("/TemplateEngine/Template/prerender.css","W");
    fwrite($RenderedCss,$compiled_file);
    fclose($RenderedCss);
}
function GetSetting($Setting){
    $con = mysqli_connect("localhost","root","bananadine777","BrickCMS")or die("Error " . mysqli_error($con));
    $query = "SELECT 'Value' FROM 'Settings' WHERE Attribute=$Setting";
    $result = mysqli_query($con,$query);
    $value = mysqli_fetch_row($result);
    return $value;
}
function MastheadDiv(){
    $radius = getSetting('MastheadCornerRadius');
    $width = getSetting('MastheadWidth');
    $height = getSetting('MastheadVPad');
    
    Css = "#Page{margin:0 auto;width:$width;height:$height;border-radius:$radius;}"
    $RenderedCss = fopen("/TemplateEngine/Template/prerender.css","W");
    fwrite($RenderedCss,$Css);
}
function RenderCMS(){
    DeletePreviousRender();
    CreateFolders();
    RenderCSS();
    MastheadDiv();
    CreateNewLines();
    MergeCSS();
    CrushCSS();
    print alert("CMS Rendered Succesfully");
}
RenderCMS();
?>